package com.kgc.service.impl;


import com.kgc.dao.*;
import com.kgc.pojo.*;
import com.kgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Autowired
    private UserDetailMapperEx userDetailMapperEx;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private ProvincesMapper provincesMapper;

    @Autowired
    private CityMapperEx cityMapperEx;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private AreasMapperEx areasMapperEx;

    @Autowired
    private AreasMapper areasMapper;

    @Autowired
    private StreetMapper streetMapper;

    @Override
    public List<TicketInfo> getTicketInfo() {
        return null;
    }

    @Override
    public List<User> getUserByLogin(String userPhone, String userPassword) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andPhoneEqualTo(userPhone).andPasswordEqualTo(userPassword);
        List<User> users = userMapper.selectByExample(userExample);
        return users;
    }

    @Override
    @Cacheable(value = "userDetail",key = "'getUserDetail'.concat(#userdetailid)")
    public UserDetail getUserDetail(Integer userdetailid) {
        return userDetailMapper.selectByPrimaryKey(userdetailid);
    }

    @Override
    @CacheEvict(value = "userDetail",allEntries = true) //清除缓存
    public int updUser_TL(UserDetail userDetail) {
        return userDetailMapper.updateByPrimaryKeySelective(userDetail);
    }

    //获取用户地址
    @Override
    public List<UserAddressEx> getAddressByUserDetailId(int userDetailId) {
        return  userDetailMapperEx.getAddressByUserDetailId(userDetailId);
    }

    @Override
    @Cacheable(value = "provinces",key = "'getProvinces_TL'")
    public List<Provinces> getProvinces_TL() {
        ProvincesExample provincesExample = new ProvincesExample();
        provincesExample.createCriteria();
        return provincesMapper.selectByExample(provincesExample);
    }

    @Override
    public List<City> getCity_TL(int province) {
        CityExample cityExample  = new CityExample();
        cityExample.createCriteria().andProvinceIdEqualTo(province);
        return cityMapper.selectByExample(cityExample);
    }

    @Override
    public List<Areas> getAreas_TL(int city) {
        AreasExample areasExample = new AreasExample();
        areasExample.createCriteria().andCityIdEqualTo(city);
        return areasMapper.selectByExample(areasExample);
    }

    @Override
    public List<Street> getStreet_TL(int area) {
        StreetExample streetExample = new StreetExample();
        streetExample.createCriteria().andAreaIdEqualTo(area);
        return  streetMapper.selectByExample(streetExample);
    }

    @Override
    @CacheEvict(value = "userDetail",allEntries = true)
    public int updUserDetail_TL(UserDetail userDetail) {
        return userDetailMapper.updateByPrimaryKeySelective(userDetail);
    }

    @Override
    @CacheEvict(value = "user")
    public int updUserPwd_TL(Integer userid, String newpwd) {
        User user = new User();
        user.setId(userid);
        user.setPassword(newpwd);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @CacheEvict(value = "userAddress",allEntries = true)
    public int addUserAddress_TL(UserAddress userAddress) {
        if(userAddress.getId() != null){
            return userAddressMapper.updateByPrimaryKeySelective(userAddress);
        }else{
            return userAddressMapper.insertSelective(userAddress);
        }

    }

    @Override
    public int delUserAddress_TL(int userAddressId) {
        return userAddressMapper.deleteByPrimaryKey(userAddressId);
    }
}
